package com.clothes.views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.clothes.R;
import com.clothes.mdoel.ClothesTable;
import com.clothes.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class EditLinearLayout extends LinearLayout {

    private List<ClothesTable> list = new ArrayList<>();
    private LinearLayout llContainer;
    private Context context;

    public EditLinearLayout(Context context) {
        super(context);
        llContainer = this;
        this.context = context;
        init();
    }

    public EditLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        llContainer = this;
        this.context = context;
        init();
    }

    private void init(){
        setData(list);
    }

    public void setList(List<ClothesTable> list) {
        this.list = list;
        setData(list);
    }

    private View getView(final int position, final ClothesTable info){
        final View view = LayoutInflater.from(context).inflate(R.layout.item_tj_table2,null);
        TextView tv1 =  view.findViewById(R.id.tv1);
        tv1.setText(position+1+"");
        TextView tv2 =  view.findViewById(R.id.tv2);
        tv2.setText(info.getModelNumber()); //款号
        TextView tv3 =  view.findViewById(R.id.tv3);
        tv3.setText(info.getSumNum()+""); //数量
        final TextView tv5 =  view.findViewById(R.id.tv5);
        tv5.setText(info.getPrice()+"");//金额
        final EditText et = view.findViewById(R.id.et4);
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE){
                    if(v.getText().toString().equals("")){
                        return true;
                    }
                    add();
                    return true;
                }
                return false;
            }
        });
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String txt = s.toString();
                double price = StringUtils.getDoubleStr(txt) * info.getSumNum();
                tv5.setText(price+"");
                if(onNumChangeListener != null){
                    onNumChangeListener.onNumChange(position,price);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        view.setTag(position);
        return view;
    }

    private void add(){
        ClothesTable info = new ClothesTable();
        list.add(info);
        View view = getView(list.size()-1,info);
        llContainer.addView(view);
    }

    private void setData(List<ClothesTable> list){
        llContainer.removeAllViews();
        for (int i=0;i<list.size();i++){
            llContainer.addView(getView(i,list.get(i)));
        }
    }

    public OnNumChangeListener onNumChangeListener;
    public interface OnNumChangeListener{
        void onNumChange(int position,double money);
    }

    public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
        this.onNumChangeListener = onNumChangeListener;
    }
}
